package com.shundr.auth.activity;

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView;
import android.widget.RelativeLayout;

import com.shundr.auth.R;
import com.shundr.auth.base.BaseActivity;
import com.shundr.auth.common.view.CListView;
import com.shundr.auth.frame.constant.LContext;
import com.shundr.auth.frame.http.LHttpRequest;
import com.shundr.auth.frame.util.ToastHelper;
import com.shundr.auth.user.adapter.AuthUserItemAdapter;
import com.shundr.auth.user.http.UserRequest;
import com.shundr.auth.user.model.UserInfo;

public class MyChannelsActivity extends BaseActivity {

	private static final int PAGE_SIZE = LContext.PAGE_SIZE;
	private int pageNo = 1;

	private CListView mListView;
	private RelativeLayout layout_no_content;
	private AuthUserItemAdapter mAdapter;

	private List<UserInfo> mList = new ArrayList<UserInfo>();

	private int status = 5;

	private Handler mHandler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case UserRequest.FIND_STAFF_USER_LIST:
				mListView.setEmptyView(layout_no_content);
				List<UserInfo> infos = (List<UserInfo>) msg.obj;
				if (infos != null) {
					if (infos.size() > 0) {
						if (pageNo == 1) {
							mList.clear();
							if (infos.size() >= PAGE_SIZE) {
								mListView.setCanLoadMore(true);
								mListView.setAutoLoadMore(true);
								mListView.setCanRefresh(true);
							} else {
								mListView.setCanLoadMore(false);
								mListView.setAutoLoadMore(false);
								mListView.setCanRefresh(false);
							}
						}

						mList.addAll(infos);
						mAdapter.notifyDataSetChanged();
						if (pageNo == 1) {
							mListView.setSelection(0);
						}
						pageNo = pageNo + 1;
					} else {
						if (pageNo != 1) {
							ToastHelper.toastLong(mContext, "最后一页");
						}
					}
				}

				break;
			case LHttpRequest.REQUEST_FAILED:
				ToastHelper.toastShort(mContext, msg.obj.toString());
				break;

			default:
				break;
			}
			mListView.onRefreshComplete();
			mListView.onLoadMoreComplete();
			super.handleMessage(msg);
		}

	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_my_channels);

		mAdapter = new AuthUserItemAdapter(mList, mContext);

		mListView = (CListView) findViewById(R.id.lv_list);
		layout_no_content = (RelativeLayout) findViewById(R.id.layout_no_content);

		mListView.setAdapter(mAdapter);
		mListView.setOnRefreshListener(new CListView.OnRefreshListener() {

			@Override
			public void onRefresh() {
				refreshList();
			}
		});
		mListView.setOnLoadListener(new CListView.OnLoadMoreListener() {

			@Override
			public void onLoadMore() {
				loadDataFromServer();
			}
		});
		mListView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View itemView,
					int position, long id) {

			}
		});

		btnRight.setText("添加新渠道");
		btnRight.setVisibility(View.VISIBLE);
		btnRight.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View arg0) {
				startActivityForResult(new Intent(mContext,
						AddNewChannelActivity.class), 500);
			}
		});

		refreshList();
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {

		if (resultCode == RESULT_OK && requestCode == 500) {
			refreshList();
		}

		super.onActivityResult(requestCode, resultCode, data);
	}

	public void refreshList() {
		pageNo = 1;
		loadDataFromServer();
	}

	public void loadDataFromServer() {
		new UserRequest(mContext, mHandler).findStaffUserInfoList(status,
				pageNo, PAGE_SIZE);
	}
}
